#pragma once

#include <iostream>
#include <time.h>

// open函数头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// write函数头文件
#include <unistd.h>
// 可变参数头文件
#include <stdarg.h>

#define LogFile "log.txt"
#define SIZE 1024

enum Level
{
    Info,
    Debug,
    Warning,
    Error,
    Fatal
};

enum PrintM
{
    Screen = 1,
    OneFile,
    ClassFile
};

class Log
{
public:
    Log()
    {
        printMethod = Screen; // 默认打印到屏幕上
        path = "./log/";      // 默认打印文件至该路径下
    }
    ~Log()
    {
    }

public:
    // 决定要将日志打印至哪里
    void Enable(int method)
    {
        printMethod = method;
    }

    // 根据日志等级获取对应的字符串
    std::string LevelToString(int level)
    {
        switch (level)
        {
        case Info:
            return "Info";
        case Debug:
            return "Info";
        case Warning:
            return "Warning";
        case Error:
            return "Error";
        case Fatal:
            return "Fatal";
        default:
            return "None";
        }
    }

    // 将信息logtxt根据不同的打印模式实现不同的代码块
    void PrintLog(int level, const std::string &logtxt)
    {
        switch (printMethod)
        {
        case Screen: // 将日志打印到屏幕上
            std::cout << logtxt << std::endl;
            break;
        case OneFile:
            PrintOneFile(LogFile, logtxt);
            break;
        case ClassFile:
            PrintClassFile(level, logtxt);
            break;
        default:
            break;
        }
    }

    // 将日志打印到一个文件中
    void PrintOneFile(const std::string &logname, const std::string &logtxt)
    {
        std::string logname_ = path + logname; // ./log/log.txt
        int fd = open(logname_.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666);
        if (fd < 0)
            return;
        write(fd, logtxt.c_str(), logtxt.size());
        close(fd);
    }

    // 将日志打印到多个文件中，按level进行分类
    void PrintClassFile(int level, const std::string &logtxt)
    {
        std::string filename = LogFile; // "log.txt"
        filename += ".";
        filename += LevelToString(level); // "log.txt.Info/Debug/Warning..."
        PrintOneFile(filename, logtxt);
    }

    // 打印日志的形式
    void operator()(int level, const char *format, ...)
    {
        time_t t = time(nullptr); // 获取时间戳
        struct tm *ctime = localtime(&t);
        char leftbuffer[SIZE];
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]",
                 LevelToString(level).c_str(), ctime->tm_year + 1900, ctime->tm_mon + 1,
                 ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
        
        // 可变参数
        va_list s;
        va_start(s, format);
        char rightbuffer[SIZE];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
        va_end(s);

        // 格式化：默认部分 + 自定义部分
        char logtxt[SIZE*2];
        snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);

        PrintLog(level, logtxt);
    
    }   

private:
    int printMethod;  // 打印位置：屏幕/一个文件/多个文件
    std::string path; // 路径
};